package abel.wlmr.model.type;

import abel.wlmr.model.WlmrException;
import abel.wlmr.model.WlmrField;

/**
 * ——异常标志：
 * ● D0：置“1”：编程次数发生变化； D0=0 时，编程次数次数无变化；
 * ● D1：置“1”： 流量传感器状态异常；置“0”： 流量传感器状态无异常；
 * ● D2： 置“1”： 压力传感器状态异常；置“0”： 压力传感器状态无异常；
 * ● D3：置“1”： 时钟变化；置“0”： 时钟无变化；
 * ● D4：置“1”：电池欠压；置“0”：电池不欠压；
 * ● D5：置“1”： 磁干扰状态变化；置“0”： 磁干扰状态无变化；
 * ● D6： ～D7 备用。
 * 
 * @author Abel
 *
 */
public class MeterExceptionField extends WlmrField {

    private byte progChanged;

    private byte flowException;

    private byte pressException;

    private byte clockChanged;

    private byte batteryLow;

    private byte magnetAttack;

    public MeterExceptionField() {
        super(1);
    }

    @Override
    public int getValue() {
        return progChanged | (flowException << 1) | (pressException << 2)
            | (clockChanged << 3) | (batteryLow << 4) | (magnetAttack << 5);
    }

    @Override
    public void parseValue(int v) throws WlmrException {
        progChanged = (byte) (v & 1);
        flowException = (byte) ((v >> 1) & 1);
        pressException = (byte) ((v >> 2) & 1);
        clockChanged = (byte) ((v >> 3) & 1);
        batteryLow = (byte) ((v >> 4) & 1);
        magnetAttack = (byte) ((v >> 5) & 1);
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return String.format(
            "MeterExceptionField [progChanged=%s, flowException=%s, pressException=%s, clockChanged=%s, batteryLow=%s, magnetAttack=%s]",
            progChanged, flowException, pressException, clockChanged, batteryLow, magnetAttack);
    }

    /**
     * @return the progChanged
     */
    public byte getProgChanged() {
        return progChanged;
    }

    /**
     * @param v
     *            the progChanged to set
     */
    public void setProgChanged(byte v) {
        if (v != 0 && v != 1) {
            throw new IllegalArgumentException("this is a bit");
        }
        this.progChanged = v;
    }

    /**
     * @return the flowException
     */
    public byte getFlowException() {
        return flowException;
    }

    /**
     * @param v
     *            the flowException to set
     */
    public void setFlowException(byte v) {
        if (v != 0 && v != 1) {
            throw new IllegalArgumentException("this is a bit");
        }
        this.flowException = v;
    }

    /**
     * @return the pressException
     */
    public byte getPressException() {
        return pressException;
    }

    /**
     * @param v
     *            the pressException to set
     */
    public void setPressException(byte v) {
        if (v != 0 && v != 1) {
            throw new IllegalArgumentException("this is a bit");
        }
        this.pressException = v;
    }

    /**
     * @return the clockChanged
     */
    public byte getClockChanged() {
        return clockChanged;
    }

    /**
     * @param v
     *            the clockChanged to set
     */
    public void setClockChanged(byte v) {
        if (v != 0 && v != 1) {
            throw new IllegalArgumentException("this is a bit");
        }
        this.clockChanged = v;
    }

    /**
     * @return the batteryLow
     */
    public byte getBatteryLow() {
        return batteryLow;
    }

    /**
     * @param v
     *            the batteryLow to set
     */
    public void setBatteryLow(byte v) {
        if (v != 0 && v != 1) {
            throw new IllegalArgumentException("this is a bit");
        }
        this.batteryLow = v;
    }

    /**
     * @return the magnetAttack
     */
    public byte getMagnetAttack() {
        return magnetAttack;
    }

    /**
     * @param v
     *            the magnetAttack to set
     */
    public void setMagnetAttack(byte v) {
        if (v != 0 && v != 1) {
            throw new IllegalArgumentException("this is a bit");
        }
        this.magnetAttack = v;
    }

}
